package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.TravelComment;
import cn.wolfcode.trip.base.mapper.TravelCommentMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ITravelCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {
    @Autowired
    private TravelCommentMapper travelCommentMapper;

    /**
     * 1.根据游记id分页查询其下的评论
     *
     * @param qo
     * @return
     */
    public PageInfo<?> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        List<TravelComment> list = travelCommentMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    /**
     * 2.保存用户提交的游记评论
     *
     * @param travelComment
     */
    public void save(TravelComment travelComment) {
        /*在业务层设置游记评论的创建时间*/
        travelComment.setCreateTime(new Date());
        travelCommentMapper.insert(travelComment);
    }

    public void update(TravelComment travelComment) {

    }

    public void saveOrUpdate(TravelComment entity) {
        if (entity.getId() == null) {
            travelCommentMapper.insert(entity);
        } else {
            travelCommentMapper.updateByPrimaryKey(entity);
        }
    }

    public List<TravelComment> list() {
        return travelCommentMapper.selectAll();
    }

    @Override
    public PageInfo<TravelComment> queryAll(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<TravelComment> list = travelCommentMapper.selectForAllList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public Integer queryCommentCount(Long uid) {
        return travelCommentMapper.selectCommentCount(uid);
    }

    @Override
    public void changeStatus(Long uid) {
        travelCommentMapper.changeCommentStatus(uid);
    }

}
